import {
  modelOptions,
  Prop,
  plugin,
  index,
  getModelForClass,
  ReturnModelType
} from '@typegoose/typegoose';
import { AutoIncrementID } from '@typegoose/auto-increment';
import { ApiProperty, OmitType, PartialType } from '@nestjs/swagger';

// 使用AutoIncrementID插件，设置userId字段自增，从1开始
@plugin(AutoIncrementID, { field: 'departmentId', startAt: 1 })
// 创建索引，username字段唯一
@index({ name: 1 }, { unique: true })
@modelOptions({
  schemaOptions: {
    //数据库集合的名称
    collection: 'department',
    //数据库创建及修改时间自动完善填充
    timestamps: true
  }
})
export class Department {
  @Prop()
  readonly departmentId: number;

  @ApiProperty({
    description: '部门名称',
    type: 'string',
    example: '技术部',
    required: true
  })
  @Prop({ required: true, trim: true, unique: true })
  name: string;

  @ApiProperty({
    description: '部门领导姓名',
    type: 'string',
    required: false,
    example: '张三'
  })
  @Prop()
  leader?: string;

  @ApiProperty({ description: '上级部门id', type: 'string', required: false })
  @Prop()
  parentId?: string;
}

// 导出UserModel，用于获取User模型
export const DepartmentModel = getModelForClass(Department);

// 定义UserModelType，用于返回User模型的类型
export type DepartmentModelReturnType = ReturnModelType<typeof Department>;

export class CreateDepartmentDto extends OmitType(Department, [
  'departmentId'
] as const) {}
export class UpdateDepartmentDto extends PartialType(CreateDepartmentDto) {}
